package com.sfx.算法专题.滑动窗口.变化窗口;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-08-05
 * Time: 16:15
 */
public class Code_3_longestOnes {

    static class Solution1 {
        public int longestOnes(int[] nums, int k) {
            int left = 0;
            int right = 0;
            int res = 0;
            int zero = 0;
            while(right < nums.length) {
                if(nums[right]==0) {
                    zero++;
                }
                right++;
                while(zero > k) {
                    if(nums[left] == 0) {
                        zero --;
                    }
                    left ++;
                }
                res = Math.max(res,right - left);
            }
            return res;
        }
    }

    static class Solution2 {
        public int longestOnes(int[] nums, int k) {
            int left = 0;
            int right = 0;
            int res = 0;
            while(right < nums.length) {
                //入窗口
                if(nums[right]==0) {
                    k--;
                }
                right++;
                //当z不满足条件时(zero的个数>k),出窗口
                while(k<0) {
                    if(nums[left] == 0) {
                        k ++;
                    }
                    left ++;
                }
                res = Math.max(res,right - left);
            }
            return res;
        }
    }

    static class Solution3 {
        public int longestOnes(int[] nums, int k) {
            int left = 0;
            int right = 0;
            int[] count = new int[2];
            while(right < nums.length) {
                count[nums[right]]++;
                right++;
                if(count[0]>k) {
                    count[nums[left]]--;
                    left ++;
                }
            }
            return right - left;
        }
    }
}